package com.voja.salonschedule.dialog;

import java.text.NumberFormat;

import com.voja.salonschedule.data.Customer.CustomerItem;
import com.voja.salonschedule.data.Event.EventItem;
import com.voja.salonschedule.Customers;
import com.voja.salonschedule.Events;
import com.voja.salonschedule.Index;
import com.voja.salonschedule.R;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class EventDialogFragment extends DialogFragment {
    
    private Context context;
    private EventItem event;
    private CustomerItem customer;
    
    private DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent i;
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    i = new Intent(context, Events.class);
                    i.putExtra("action", "edit");
                    i.putExtra("id", ""+event.id);
                    context.startActivity(i);
                    break;
            }
        }
    };
    
    public void setData(Context context, EventItem event, CustomerItem customer) {
        this.context  = context;
        this.event    = event;
        this.customer = customer;
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    	Activity activity = ((Activity)context);
        AlertDialog.Builder builder;
    	if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
    		builder = new AlertDialog.Builder(context);
    	} else {
    		builder = new AlertDialog.Builder(activity, AlertDialog.THEME_HOLO_LIGHT);
    	}
        LayoutInflater inflater = activity.getLayoutInflater();
        
        View v = inflater.inflate(R.layout.event_show, null);
        loadData(v);
        
        builder
            .setView(v)
            .setPositiveButton(R.string.Edit, clickListener)
            .setNegativeButton(R.string.Close, clickListener);
        
        return builder.create();
    }
    
    public void loadData(View v) {
        if (customer == null) {
            return;
        }
        
        String price = NumberFormat.getCurrencyInstance().format(event.price);
        
        setTextviewText(v, R.id.txtService, event.service);
        setTextviewText(v, R.id.txtPrice, price);
        setTextviewText(v, R.id.txtCustomer, customer.name);
        
        setTextviewText(v, R.id.txtPhone, customer.phone);
        
        ImageView imgCustomer = (ImageView)v.findViewById(R.id.imgCustomer);
        if (String.valueOf(customer.name).length()>0) {
            imgCustomer.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(context, Customers.class);
                    i.setAction(Intent.ACTION_EDIT);
                    i.putExtra(Customers.KEY_CUSTOMER_NAME, customer.name);
                    context.startActivity(i);
                }
            });
        }
        if (customer.image != null && customer.image.length>0) {
            imgCustomer.setImageBitmap(BitmapFactory.decodeByteArray(customer.image, 0, customer.image.length));
        }
        
        LinearLayout phoneActions = (LinearLayout)v.findViewById(R.id.phoneActions);
        if (customer.hasPhone()) {
        	phoneActions.setVisibility(View.VISIBLE);
        	
        	// Dial image
        	ImageView imgCall = (ImageView)v.findViewById(R.id.imgCall);
        	imgCall.setContentDescription(customer.phone);
        	imgCall.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					String number = v.getContentDescription().toString();
					Index.makeCall(context, number);
				}
			});
        	
        	// Message image
        	ImageView imgMessage = (ImageView)v.findViewById(R.id.imgMessage);
        	imgMessage.setContentDescription(customer.phone);
        	imgMessage.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					String number = v.getContentDescription().toString();
					Index.sendMessage(context, number);
				}
			});
        } else {
        	phoneActions.setVisibility(View.GONE);
        }
    }
    
    private TextView setTextviewText(View v, int id, String text) {
        TextView tv = (TextView)v.findViewById(id);
        tv.setText(text);
        
        return tv;
    }

}
